#include "feedback.h"
#include "fb.h"
#include <assert.h>
#include <string.h>

int feedback_get_state(MH_UDP *pudp, FEEDBACK *fb, int type)
{
    assert(fb != NULL);

    int error_state = 0;
    int sensor_state = 0;
    int ret = 0;

    /* 错误反馈 */
    ret = fb_get_error_state(pudp, &error_state, type);

    if (ret < 0)
    {
        return ret;
    }

    memcpy(&fb->error, &error_state, sizeof (error_state));

    /* 感应器反馈 */
    if (fb_get_sensor_state(pudp, &sensor_state, type) < 0)
    {
        return -1;
    }
    memcpy(&fb->sensor, &sensor_state, sizeof (sensor_state));

    return 0;
}

int feedback_get_error_state(MH_UDP *pudp, FEEDBACK *fb, int type)
{
    assert(fb != NULL);

    int error_state = 0;
    int ret = 0;

    /* 错误反馈 */
    ret = fb_get_error_state(pudp, &error_state, type);

    if (ret < 0)
    {
        return ret;
    }

    if (ret == 0)
    {
        memcpy(&fb->error, &error_state, sizeof (error_state));
    }

    return 0;
}


int feedback_get_sensor_state(MH_UDP *pudp, FEEDBACK *fb, int type)
{
    assert(fb != NULL);

    int sensor_state = 0;
    int ret = 0;

    /* 感应器反馈 */
    ret = fb_get_sensor_state(pudp, &sensor_state, type);

    if (ret < 0)
    {
        return ret;
    }

    memcpy(&fb->sensor, &sensor_state, sizeof (sensor_state));

    return 0;
}

int feedback_get_crg_state(MH_UDP *pudp, CRGSTATE *crgstate)
{
    assert(crgstate != NULL);

    return fb_get_crg_state(pudp, crgstate, 0);
}
